/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-09-22 10:37:46
 * @LastEditors: zhl
 * @LastEditTime: 2022-01-20 15:42:16
 * @Description: 文件实现描述
 */
#ifndef __HANDLE_THREAD_MANAGER_H__
#define __HANDLE_THREAD_MANAGER_H__

#include <pthread.h>
#include <utility>
#include <unordered_map>

#include "../../HPLCProxy/include/HPLCProxy.h"
#include "IChannel.h"
#include "CConfigManager.h"
#include "CThreadNotify.h"
#include "SerialProxy.h"
#include "CMosquittoManager.h"
#include "global_data_struct.h"

/**
 * @brief 针对接口启动的处理类
 * 
 */
class HandleThreadManager{
private:
    uint8_t  *c_recv_data;
    bool c_is_run;
    pthread_t c_pthread;
    CThreadNotify  c_notify;
    std::shared_ptr<CConfigManager> c_cm;
    std::shared_ptr<SerialProxy> c_sp_ptr;
    std::shared_ptr<HPLCProxy>   c_hplc_ptr;

    int     c_comm_type;
private:
    void push_task_item(TASK_PARAMS &dev, std::unordered_map<int64_t, std::list<TASK_PARAMS>> &devs);
    int  run(std::shared_ptr<TASK_PARAMS> dev);
    void *get_func(std::string protocol_type, std::string &&func_name);
    void init_serial(std::string &addr);
    void wait(int ms);
    void clear_task_items();

    void init_hplc();
public:
    std::shared_ptr<CMosquittoManager> c_mm; 
    std::unordered_map<int64_t, std::list<TASK_PARAMS>> c_devs;
    //<优先级,<设备id,<任务项id, >>>
    std::unordered_map<int64_t, std::list<TASK_PARAMS>> c_cmd_devs;
public:
    HandleThreadManager(std::shared_ptr<CConfigManager> cm, std::shared_ptr<CMosquittoManager> mm, std::string addr, int comm_type = PORT_TYPE_SERIAL);
    ~HandleThreadManager();

    int execute();
    void stop();
    static void *handle_thread(void *arg);
    void *response_callback(void *arg);
    bool is_exists_task_item();
    void add_task_items(TASK_PARAMS &dev);
    void add_cmd_task_items(TASK_PARAMS &dev);
    std::shared_ptr<TASK_PARAMS> get_dev_task_items(std::unordered_map<int64_t, std::list<TASK_PARAMS>> &devs);
};




#endif /* __HANDLE_THREAD_MANAGER_H__ */
